package org.android.demo;

import java.util.ArrayList;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

/**
 * This is your own Adapter implementation for SpinnerAdapter
 */
class MyAdapter implements SpinnerAdapter {

	/**
	 * The internal data (the ArrayList with the Objects).
	 */
	ArrayList<String> data;
	Context mContext;

	public MyAdapter(ArrayList<String> data, Context mContext){
		this.data = data;
		this.mContext = mContext;
	}

	/**
	 * Returns the View that is shown when a element was selected.
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TextView mTextView;

		// for efficiency we use 'convertView' so that we don't have to create each time a new View
		if (convertView == null) {
			LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			mTextView = (TextView)vi.inflate(android.R.layout.simple_spinner_item, null);
		} else
			mTextView = (TextView)convertView;

		mTextView.setTextColor(Color.BLACK);
		mTextView.setText(data.get(position));

		return mTextView;
	}

	/**
	 * The Views which are shown in when the arrow is clicked
	 */
	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		TextView mTextView;

		// for efficiency we use 'convertView' so that we don't have to create each time a new View
		if (convertView == null) {
			LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			mTextView = (TextView)vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
		} else
			mTextView = (TextView)convertView;

		mTextView.setTextColor(Color.BLACK);
		mTextView.setText(data.get(position));

		return mTextView;
	}

	/**
	 * Returns the Size of the ArrayList
	 */
	@Override
	public int getCount() {
		return data.size();
	}

	/**
	 * Returns one Element of the ArrayListat the specified position.
	 */
	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		return android.R.layout.simple_spinner_item;
	}

	@Override
	public boolean isEmpty() {
		return (data.size() == 0);
	}

	@Override
	public int getViewTypeCount() {
		return 1;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
	}

}
